package com.rjwh.dingdong.client.activity.image;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.adapter.ImageShowPagerAdapter;
import com.rjwh.dingdong.client.widget.HackyViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 照片浏览界面
 *
 * @author 王博扬
 * @ClassName ImageListBrowseActivity
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2015-8-24 下午3:09:36
 * @history 1.YYYY-MM-DD author: description:
 */
public class ImageListBrowseActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.image_pager_browse_pager)
    HackyViewPager pager;

    private ArrayList<String> pathList;
    private ArrayList<String> photos;
    private boolean isDoDelete = false;
    private int pos;
    private boolean isAvatar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_pager_browse);
        ButterKnife.bind(this);
        parseIntent();
        if (!isAvatar) {
            initToolbarHelper();
        }
        initPager(photos, pathList, pos);
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            if (intent.hasExtra("list")) {
                photos = ((ArrayList<String>) intent.getSerializableExtra("list"));
                isAvatar = intent.getBooleanExtra("isAvatar", false);
            }
            if (intent.hasExtra("pathlist")) {
                pathList = ((ArrayList<String>) intent.getSerializableExtra("pathlist"));
                isDoDelete = true;
            }
            pos = intent.getIntExtra("position", 0);
        }
    }

    /**
     * init the toolbar
     */
    protected void initToolbarHelper() {
        appBarLayout.setVisibility(View.VISIBLE);
        toolbar.setBackgroundColor(Color.BLACK);
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(isDoDelete ? pos + 1 + "/" + pathList.size() : pos + 1 + "/" + photos.size());
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setBackData(pathList);
            }
        });
    }

    public void initPager(final ArrayList<String> photos, final ArrayList<String> pathList, final int pos) {
        pager.setAdapter(new ImageShowPagerAdapter(photos, pathList, ImageListBrowseActivity.this));
        pager.setCurrentItem(pos);
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                ImageListBrowseActivity.this.pos = position;
            }

            @Override
            public void onPageSelected(int position) {
                if (getSupportActionBar() != null) {
                    getSupportActionBar().setTitle(isDoDelete ? position + 1 + "/" + pathList.size() : position + 1 + "/" + photos.size());
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 返回并传递数据到上一个界面
     */
    private void setBackData(final ArrayList<String> pathList) {
        if (isDoDelete) {
            Intent intent = new Intent();
            intent.putStringArrayListExtra("pathList", pathList);
            setResult(RESULT_OK, intent);
        }
        ImageListBrowseActivity.this.finish();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.gc();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            setBackData(pathList);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.getMenuInflater().inflate(R.menu.menu_image_confim, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuItem menuItem = menu.findItem(R.id.menu_confim);
        if (isDoDelete) {
            menuItem.setVisible(true);
            menuItem.setTitle("删除");
        } else {
            menuItem.setVisible(false);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_confim:
                isDoDelete = true;
                pathList.remove(pos);
                if (pathList.size() > 0) {
                    if (pos == 0) {//只有一张图片
                        initPager(null, pathList, pos);
                        if (getSupportActionBar() != null) {
                            getSupportActionBar().setTitle(1 + "/" + pathList.size());
                        }
                    } else {
                        initPager(null, pathList, pos - 1);
                        if (getSupportActionBar() != null) {
                            getSupportActionBar().setTitle(pos + "/" + pathList.size());
                        }
                    }
                } else {
                    setBackData(pathList);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
